package com.black.cat.seed.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.black.cat.seed.common.ApiResponse;
import com.black.cat.seed.entity.Role;
import com.black.cat.seed.entity.User;
import com.black.cat.seed.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author blackcat
 * @create 2021/4/4 16:04
 * @version: 1.0
 * @description:TODO  用户管理角色接口待添加
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/get")
    public ApiResponse<Role> get(Long id) {
        return ApiResponse.ofSuccess(roleService.getById(id));
    }

    @GetMapping("page")
    public ApiResponse<List<Role>> page(Role entity, Integer pageNo, Integer pageSize) {
        IPage<Role> page = new Page<>(pageNo, pageSize);
        IPage<Role> list = roleService.page(page, new QueryWrapper<>(entity));
        return ApiResponse.ofSuccess(list.getRecords(), list.getTotal());
    }

    @PostMapping("create")
    public ApiResponse<Boolean> create(Role entity) {
        return ApiResponse.ofSuccess(roleService.save(entity));
    }

    @PostMapping("update")
    public ApiResponse<Boolean> update(@Validated() Role entity) {
        return ApiResponse.ofSuccess(roleService.updateById(entity));
    }

    @PostMapping("/delete")
    public ApiResponse<Boolean> delete(Long id) {
        return ApiResponse.ofSuccess(roleService.removeById(id));
    }

}
